var header = {
  // 'content-type': 'application/x-www-form-urlencoded',
  // 'content-type': 'application/json', // 默认值
}
// let baseUrl = 'http://home.ynims.com:8082'
let baseUrl = 'https://home.ynims.com'
//  let baseUrl = 'http://39.108.253.88:8082'
class Ajax {
  static post(url, params, headers = {}) {
    return Ajax.request(url, params, "POST", headers);
  }
  static get(url, params, headers = {}) {
    return Ajax.request(url, params, "GET", headers);
  }
  static upload(url, filePath, params, headers = {}) {
    let wxAccessToken = wx.getStorageSync('wxAccessToken');
    let _header = Object.assign({}, header, headers, {
      wxAccessToken,
      "Content-Type": "multipart/form-data"
    })
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: baseUrl + url, //仅为示例，非真实的接口地址
        filePath: filePath,
        header: _header,
        name: 'imgs',
        formData: params,
        success(res) {
          if (res.data) {
            if (res.statusCode == 200) {
              resolve(JSON.parse(res.data))
            } else {
              reject(res.data);
            }
          }
        },
        fail(error) {
          reject(error);
        }
      })
    })

  }
  static request(url, params, method, headers = {}) {
    let wxAccessToken = wx.getStorageSync('wxAccessToken');
    let _header = Object.assign({}, header, headers, {
      wxAccessToken
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + url,
        data: params,
        method: method,
        header: _header,
        success: function(res) {
          if (res.data) {
            if (res.statusCode == 200) {
              if (res.data.code == 102) {
                wx.redirectTo({
                  url: '/pages/authorization/authorization',
                })
              } else {
                resolve(res.data)
              }
            } else {
              reject(res.data);
            }
          }
        },
        fail: function(error) {
          reject(error);
        }
      })
    })
  }
}
export default Ajax;